package com.lqz.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.RequestMapping;  
import com.lqz.model.ActiveUser;
import com.lqz.service.UserService;
  
@Controller  
@RequestMapping("cacheTest")  
public class UserController {  
    @Resource  
    private UserService userService;  
      
  //登陆提交地址，和applicationContext-shiro.xml中配置的loginurl一致
  		@RequestMapping("/checkLogin")
  		public String checkLogin(HttpServletRequest request,Model model) throws Exception{
  			
  			//如果登陆失败从request中获取认证异常信息，shiroLoginFailure就是shiro异常类的全限定名
  			String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
  			String error=null;
  			//根据shiro返回的异常类路径判断，抛出指定异常信息
  			if(exceptionClassName!=null){
  				if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
  					error="账号不存在";
  					//最终会抛给异常处理器
  					//throw new CustomException("账号不存在");
  				} else if (IncorrectCredentialsException.class.getName().equals(
  						exceptionClassName)) {
  					error="用户名/密码错误";
  					//throw new CustomException("用户名/密码错误");
  				} else if (LockedAccountException.class.getName().equals(
  						exceptionClassName)) {
  					error="用户被锁定不可用";
  					//throw new CustomException("用户名/密码错误");
  				} else if("randomCodeError".equals(exceptionClassName)){
  					//throw new CustomException("验证码错误 ");
  					error="验证码错误";
  				}else {
  					error="未知错误";
  					//throw new Exception();//最终在异常处理器生成未知错误
  				}
  			}
  			Subject subject = SecurityUtils.getSubject();  
  			if(subject.isAuthenticated()){
  				ActiveUser activeUser=(ActiveUser) subject.getPrincipal();
  				if(activeUser!=null){
  					String clietOsName=request.getHeader("user-agent");
  					String url=null;
  					if(clietOsName.matches(".*Windows NT.*") || clietOsName.matches(".*Linux.*") ){
  						url="/index.jsp";
  					}else{
  						url="";
  					}
  					model.addAttribute("activeUser", activeUser);
  					model.addAttribute("url", url);
  					//return "redirect:/auth/getMenus.do";
  				}
  			}
  			model.addAttribute("error", error);

  			//此方法不处理登陆成功（认证成功），shiro认证成功会自动跳转到上一个请求路径
  			//登陆失败还到login页面
  			return "login";
//  			return "/com/sys/homePage";
//  			return "redirect:getMenus.do";
  		}
  		
    @RequestMapping(value="/get")  
    public String get(String userNo, Model model){ 
    	System.out.println("----------"+userNo+"------------");
        String username = userService.get(userNo);  
        model.addAttribute("username", username);  
        return "getUser";  
    }  
      
    @RequiresPermissions("search:1")
    @RequestMapping(value="/update")  
    public String update( String userNo, Model model){  
        userService.update(userNo);  
        model.addAttribute("userNo", userNo);  
        return "updateUser";  
    }  
      
    @RequestMapping(value="/removeAll")  
    public String removeAll(){  
        userService.removeAll();  
        return "removeAllUser";  
    }  
}  